package cn.crc.web.net;

import org.apache.commons.net.ftp.FTPClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 客户端访问FTP工具_25.4.25_liuhz
 */
public class TestFTPClient {
    public static void main(String[] args) {
        TestFTPClient.testUpload();
        TestFTPClient.testDownload();
    }

    private static void testUpload(){
        String remoteFile= "测试文件23.txt";
        String localFile= "F:\\CMD产品数据平台.docx";//"F:\\conf_book.txt";
        TestFTPClient.fileUpload(TestFTPClient.getFtpConn(), remoteFile, localFile);
    }
    private static void testDownload(){
        String remoteFile= "堡垒机.rar";
        String localFile= "F:\\武清堡垒机.rar";
        TestFTPClient.fileDownload(TestFTPClient.getFtpConn(), remoteFile, localFile);
    }

    public static FTPClient getFtpConn(){
        String server= "127.0.0.1";//""192.168.0.121";
        int port= 21;
        String username= "ftpadmin";
        String password= "123456";
        FTPClient ftpClient= new FTPClient();
        try {
            ftpClient.connect(server, port);
            ftpClient.login(username, password);
            ftpClient.enterLocalPassiveMode();//ftpClient.enterLocalActiveMode();//每次client连接告诉server开通一个端口
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.setControlEncoding(StandardCharsets.UTF_8.toString());
            //ftpClient.setBufferSize(2048);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ftpClient;
    }

    public static void fileUpload(FTPClient ftpClient, String remoteFile, String localFile){
        FileInputStream inputStream= null;
        String filePathRemote= remoteFile;//远程文件
        String filePathLocal= localFile;
        try {
            filePathRemote= new String(filePathRemote.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);//远程文件要编码
            inputStream= new FileInputStream(new File(filePathLocal));

            //System.out.println("工作目录~"+ftpClient.changeWorkingDirectory("/test"));
            if(ftpClient.changeWorkingDirectory("/test")){
                if(ftpClient.storeFile(filePathRemote, inputStream)){
                    System.out.println("文件上传成功");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
                if(inputStream!=null) inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private static void fileUpload2(){
        String server= "127.0.0.1";//10.100.115.39
        int port= 21;
        String username= "ftpadmin";
        String password= "123456";
        FTPClient ftpClient= new FTPClient();
        FileInputStream inputStream= null;
        try {
            ftpClient.connect(server, port);
            //ftpClient.login(username, password);
            if(ftpClient.login(username, password)){
                System.out.println("连接成功-"+ftpClient);
            }
            ftpClient.enterLocalPassiveMode(); //ftpClient.enterLocalActiveMode();//每次client连接告诉server开通一个端口
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.setControlEncoding(StandardCharsets.UTF_8.toString());
            //ftpClient.setBufferSize(2048);

            String filePathRemote= "aaa.txt";//远程文件
            filePathRemote= new String(filePathRemote.getBytes("GBK"),StandardCharsets.ISO_8859_1);//远程文件要编码
            String filePathLocal= "F:\\conf_book.txt";
            inputStream= new FileInputStream(new File(filePathLocal));

            System.out.println("工作目录-"+ftpClient.changeWorkingDirectory("/test"));
            if(ftpClient.changeWorkingDirectory("/test")){
                System.out.println("工作目录存在");
                if(ftpClient.storeFile(filePathRemote, inputStream)){
                    System.out.println("文件上传成功");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
                if(inputStream!=null) inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void fileDownload(FTPClient ftpClient, String remoteFile, String localFile){
        OutputStream outputStream= null;
        try {
            ftpClient.enterLocalActiveMode();
            outputStream= Files.newOutputStream(Paths.get(localFile));
            String filename= new String(remoteFile.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);//解决中文问题
            ftpClient.retrieveFile(filename, outputStream);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                outputStream.close();
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
